package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessNatixis {
    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("Natexis Bleichroeder Inc.");
                    
                    reports.add(report);
                }
            }

            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        Pattern patternVersion = Pattern.compile("Natixis_File\\s([0-9]+)[_]{0,1}");
        Matcher matcher = patternVersion.matcher(report.getFileName());
        if (matcher.find()) {
            getAnalysisReportDetailVersion(report);
            int version = Integer.parseInt(matcher.group(1));
            if(version <= 4)
                getAnalysisReportDetailVersion(report);
            else {
                if(version == 5 && report.getStartPage() < 188)
                    getAnalysisReportDetailVersion(report);
                else
                    getAnalysisReportDetailVersionAfter4(report);
            }
        }
    }
    public static void getAnalysisReportDetailVersion(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }
        patternDate = Pattern.compile("(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Analyst name
        Pattern patternTel = Pattern.compile("Research Associate|Senior Analyst|Analyst");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherTel = patternTel.matcher(line);
            if (matcherTel.find()) {
                report.addAnalystName(lines[i - 1]);
            }
        }

        //Ticker and Current price
        Pattern patternTicker = Pattern.compile("([A-Z]+)\\s(\\$[0-9]+\\.[0-9]+)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherTicker = patternTicker.matcher(line);
            if (matcherTicker.find()) {
                report.setTicker(matcherTicker.group(1));
                report.setCurrentPrice(matcherTicker.group(2));
                break;
            }
        }
        if(report.getTicker().equals("")){
            Pattern patternBloomberg = Pattern.compile("Bloomberg");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherBloomberg = patternBloomberg.matcher(line);
                if (matcherBloomberg.find()) {
                    String originTicker = lines[i + 1];
                    report.setTicker(originTicker.split(" ")[1]);
                    break;
                }
            }
        }
        if(report.getCurrentPrice() == null){
            Pattern patternDate2 = Pattern.compile("[0-9]+/[0-9]+/[0-9]+");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate2 = patternDate2.matcher(line);
                if (matcherDate2.find()) {
                    report.setCurrentPrice(lines[i + 1]);
                    if(report.getDate() == null || report.getDate().equals(""))
                        report.setDate(matcherDate2.group());
                    break;
                }
            }
        }
        if(report.getCurrentPrice() == null || report.getCurrentPrice().equals("")){
            patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate2 = patternDate.matcher(line);
                if (matcherDate2.find()) {
                    report.setCurrentPrice(lines[i + 1]);
                    break;
                }
            }
        }
        if(report.getDate() == null || report.getDate().equals("")){
            patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate2 = patternDate.matcher(line);
                if (matcherDate2.find()) {
                    report.setDate(matcherDate2.group() + " " + lines[i + 1]);
                    report.setCurrentPrice(lines[i + 2]);
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            patternDate = Pattern.compile("(Jan\\.|Feb\\.|Mar\\.|Apr\\.|May|Jun\\.|Jul\\.|Aug\\.|Sep\\.|Oct\\.|Nov\\.|Dec\\.){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate2 = patternDate.matcher(line);
                if (matcherDate2.find()) {
                    report.setDate(matcherDate2.group());
                    report.setCurrentPrice(lines[i + 1]);
                    break;
                }
            }
        }

        //Fair Value
        Pattern patternFairValue = Pattern.compile("Fair Value:\\s(\\$[0-9]+(\\.[0-9]+)?)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherFairValue = patternFairValue.matcher(line);
            if (matcherFairValue.find()) {
                report.setFairValue(matcherFairValue.group(1));
                break;
            }
        }
        if(report.getFairValue() == null){
            patternFairValue = Pattern.compile("fair value");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherFairValue = patternFairValue.matcher(line);
                if (matcherFairValue.find()) {
                    report.setFairValue(lines[i + 1]);
                    break;
                }
            }
        }

        //Previous
        Pattern patternPrevious = Pattern.compile("\\[Previous:\\s(\\$[0-9]+(\\.[0-9]+)?)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherPrevious = patternPrevious.matcher(line);
            if (matcherPrevious.find()) {
                report.setPreviousPrice(matcherPrevious.group(1));
                break;
            }
        }
        if(report.getPreviousPrice() == null){
            patternPrevious = Pattern.compile("\\(Previous:\\s(\\$[0-9]+(\\.[0-9]+)?)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherPrevious = patternPrevious.matcher(line);
                if (matcherPrevious.find()) {
                    report.setPreviousPrice(matcherPrevious.group(1));
                    break;
                }
            }
        }

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
        lines = content.split("\r\n");
        List<String> disclosures = new ArrayList<String>();
        List<String> temp = new ArrayList<String>();
        String disclosure = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("has a financial interest")) {
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m >= 0; m--){
                    disclosure += temp.get(m) + " ";
                }
                temp.clear();
                disclosure += line + " ";
                while(!lines[j].contains(".") && j < lines.length){
                    disclosure += lines[j] + " ";
                    j++;
                }
                if(j < lines.length)
                    disclosure += lines[j] + " ";
            }
        }
        Pattern patternSentence = Pattern.compile(".*([A-Z][a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+.*)");
        String[] values = disclosure.split("\\.");
        for(String value : values){
            System.out.println(value);
            Matcher matcherSentence = patternSentence.matcher(value);
            if(matcherSentence.find() && value.contains("has a financial interest")){
                disclosures.add(value);
                break;
            }
        }

        if(disclosures.size() == 0){
            for(String value : values){
                if(value.contains("has a financial interest")){
                    disclosures.add(value);
                    break;
                }
            }
        }

        report.setOwnershipDisclosures(disclosures);

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(),report.getStartPage(),report.getStartPage());
        lines = content.split("\r\n");

        //Company Name
        Pattern patternCompany = Pattern.compile("(Incised901NdBT\\]<21\\.0>|Arial\\]<50\\.0>|ArialMT\\]<51\\.0>|Arial\\]<47\\.0>|Arial\\]<44\\.0>|Arial\\]<35\\.0>)(.*)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherCompany = patternCompany.matcher(line);
            if (matcherCompany.find()) {
                String originCompany = matcherCompany.group(2);
                if(originCompany.contains("[")){
                    int end = originCompany.indexOf("[");
                    report.setCompanyName(originCompany.substring(0,end));
                }
                else{
                    if(!lines[i + 1].startsWith("[")){
                        String another = lines[i + 1];
                        if(another.contains("[")){
                            int end = another.indexOf("[");
                            another = another.substring(0,end);
                        }
                        report.setCompanyName(originCompany + " " + another);
                    }
                    else
                        report.setCompanyName(originCompany);
                }
                if(lines[i + 1].contains("Initiating Coverage"))
                    report.setInitiatingCoverage("true");
                else
                    report.setInitiatingCoverage("false");
                break;
            }
        }

        if(report.getCompanyName() == null || report.getCompanyName().equals("")){
            patternCompany = Pattern.compile("\\[\\S*>[A-Za-z]+");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                if(line.contains("Natexis Bleichroeder Inc.")){
                    for(int j = i + 1; j < lines.length; j++){
                        Matcher matcherCompany = patternCompany.matcher(lines[j]);
                        if(matcherCompany.find()){
                            String originCompany = lines[j];
                            if(originCompany.contains(">")){
                                int begin = originCompany.indexOf(">");
                                report.setCompanyName(originCompany.substring(begin + 1));
                            }
                            else{
                                if(!lines[j + 1].startsWith("[")){
                                    String another = lines[j + 1];
                                    if(another.contains("[")){
                                        int end = another.indexOf("[");
                                        another = another.substring(0,end);
                                    }
                                    report.setCompanyName(originCompany + " " + another);
                                }
                                else {
                                    report.setCompanyName(originCompany);
                                    if(lines[j + 1].contains("Initiating Coverage"))
                                        report.setInitiatingCoverage("true");
                                    else
                                        report.setInitiatingCoverage("false");
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        report.setPriorRecommendation("");
        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetailVersionAfter4(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }
        patternDate = Pattern.compile("(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Analyst name
        Pattern patternTel = Pattern.compile("Research Associate|Senior Analyst|Analyst");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherTel = patternTel.matcher(line);
            if (matcherTel.find()) {
                report.addAnalystName(lines[i - 1]);
            }
        }

        //Ticker
        Pattern patternTicker = Pattern.compile("([A-Z]+):\\s([A-Z\\S]+)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherTicker = patternTicker.matcher(line);
            if (matcherTicker.find()) {
                report.setTicker(matcherTicker.group(1));
                break;
            }
        }
        if(report.getTicker() == null || report.getTicker().equals("CONTENTS")){
            patternTicker = Pattern.compile("CONTENTS:\\s(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherTicker = patternTicker.matcher(line);
                if (matcherTicker.find()) {
                    String value = matcherTicker.group(1);
                    report.setTicker(value.split(",")[0]);
                    break;
                }
            }
        }

        //Current price
        Pattern patternCurrentPrice = Pattern.compile("Current Price");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherCurrentPrice = patternCurrentPrice.matcher(line);
            if (matcherCurrentPrice.find()) {
                report.setCurrentPrice(lines[i + 1]);
            }
        }
        if(report.getCurrentPrice() == null || report.getCurrentPrice().equals("")){
            Pattern patternPrice = Pattern.compile("Price:\\s*(\\$[0-9]+\\.[0-9]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherCurrentPrice = patternPrice.matcher(line);
                if (matcherCurrentPrice.find()) {
                    report.setCurrentPrice(matcherCurrentPrice.group(1));
                }
            }
        }

        //Fair Value
        Pattern patternFairValue = Pattern.compile("Fair Value:\\s(\\$[0-9]+(\\.[0-9]+)?)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherFairValue = patternFairValue.matcher(line);
            if (matcherFairValue.find()) {
                report.setFairValue(matcherFairValue.group(1));
                break;
            }
        }
        if(report.getFairValue() == null){
            patternFairValue = Pattern.compile("fair value");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherFairValue = patternFairValue.matcher(line);
                if (matcherFairValue.find()) {
                    report.setFairValue(lines[i + 1]);
                    break;
                }
            }
        }

        //Previous
        Pattern patternPrevious = Pattern.compile("\\[Previous:\\s(\\$[0-9]+(\\.[0-9]+)?)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherPrevious = patternPrevious.matcher(line);
            if (matcherPrevious.find()) {
                report.setPreviousPrice(matcherPrevious.group(1));
                break;
            }
        }
        if(report.getPreviousPrice() == null){
            patternPrevious = Pattern.compile("\\(Previous:\\s(\\$[0-9]+(\\.[0-9]+)?)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherPrevious = patternPrevious.matcher(line);
                if (matcherPrevious.find()) {
                    report.setPreviousPrice(matcherPrevious.group(1));
                    break;
                }
            }
        }

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
        lines = content.split("\r\n");
        List<String> disclosures = new ArrayList<String>();
        List<String> temp = new ArrayList<String>();
        String disclosure = "";
        for(int i = 0; i < lines.length; i++){
            String line = lines[i].trim();
            if(line.contains("has a financial interest")) {
                int j = i + 1, k = i - 1;
                //往上和往下找整句
                while(!lines[k].contains(".") && k >= 0){
                    temp.add(lines[k]);
                    k--;
                }
                if(k >= 0)
                    temp.add(lines[k]);
                for(int m = temp.size() - 1; m >= 0; m--){
                    disclosure += temp.get(m) + " ";
                }
                temp.clear();
                disclosure += line + " ";
                while(!lines[j].contains(".") && j < lines.length){
                    disclosure += lines[j] + " ";
                    j++;
                }
                if(j < lines.length)
                    disclosure += lines[j] + " ";
            }
        }
        Pattern patternSentence = Pattern.compile(".*([A-Z][a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+\\s[a-z\\S]+.*)");
        String[] values = disclosure.split("\\.");
        for(String value : values){
            System.out.println(value);
            Matcher matcherSentence = patternSentence.matcher(value);
            if(matcherSentence.find() && value.contains("has a financial interest")){
                disclosures.add(value);
                break;
            }
        }

        if(disclosures.size() == 0){
            for(String value : values){
                if(value.contains("has a financial interest")){
                    disclosures.add(value);
                    break;
                }
            }
        }

        report.setOwnershipDisclosures(disclosures);

        content = FileUtil.pdfReaderWithFontsAndColor(report.getFilePath(),report.getStartPage(),report.getStartPage());
        lines = content.split("\r\n");

        //Company Name
        Pattern patternCompany = Pattern.compile("(ArialMT\\]<40\\.0>)(.*)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherCompany = patternCompany.matcher(line);
            if (matcherCompany.find()) {
                String originCompany = matcherCompany.group(2);
                if(originCompany.contains("[")){
                    int end = originCompany.indexOf("[");
                    report.setCompanyName(originCompany.substring(0,end));
                }
                else{
                    if(!lines[i + 1].startsWith("[")){
                        String another = lines[i + 1];
                        if(another.contains("[")){
                            int end = another.indexOf("[");
                            another = another.substring(0,end);
                        }
                        report.setCompanyName(originCompany + " " + another);
                    }
                    else
                        report.setCompanyName(originCompany);
                }
                if(lines[i + 1].contains("Initiating Coverage"))
                    report.setInitiatingCoverage("true");
                else
                    report.setInitiatingCoverage("false");
                break;
            }
        }

        if(report.getCompanyName() == null || report.getCompanyName().equals("")){
            patternCompany = Pattern.compile("\\[\\S*>[A-Za-z]+");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                if(line.contains("Natexis Bleichroeder Inc.")){
                    for(int j = i + 1; j < lines.length; j++){
                        Matcher matcherCompany = patternCompany.matcher(lines[j]);
                        if(matcherCompany.find()){
                            String originCompany = lines[j];
                            if(originCompany.contains(">")){
                                int begin = originCompany.indexOf(">");
                                report.setCompanyName(originCompany.substring(begin + 1));
                            }
                            else{
                                if(!lines[j + 1].startsWith("[")){
                                    String another = lines[j + 1];
                                    if(another.contains("[")){
                                        int end = another.indexOf("[");
                                        another = another.substring(0,end);
                                    }
                                    report.setCompanyName(originCompany + " " + another);
                                }
                                else {
                                    report.setCompanyName(originCompany);
                                    if(lines[j + 1].contains("Initiating Coverage"))
                                        report.setInitiatingCoverage("true");
                                    else
                                        report.setInitiatingCoverage("false");
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        report.setPriorRecommendation("");
        System.out.println(JSON.toJSON(report));
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] Files = new File(rootDir).listFiles();
        for (File file : Files) {
            if(file.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(file.getPath()));
        }

        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Current price");
        headers.add("Fair value");
        headers.add("Previous price");
        headers.add("Ownership disclosure");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        headers.add("Exchange");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getCurrentPrice(),
                    report.getFairValue(),
                    report.getPreviousPrice(),
                    JSON.toJSONString(report.getOwnershipDisclosures()),
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + "",
                    report.getExchange(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation()};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
